﻿@page "/projects"
@inject IProjectService ProjectService
<PageTitle>项目列表</PageTitle>
<!--sticky固定位置，top: 3.5rem-->
<div style="position: sticky; top: 3.5rem; z-index: 1;background-color:White">
    <div class="row position-relative">
        <div class="col input-group mb-2">
            <span class="input-group-text">搜索</span>
            <input class="form-control" placeholder="Search" 
                   @bind-value="@_search" @oninput="@Search" />
        </div>
        <div class="col form-check ms-3 mt-2" style="max-width: 70px">
            <input class="form-check-input"
                   type="checkbox"
                   value="@_annual"
                   @onchange="OnAnnualChange">
            <label class="form-check-label">
                全年
            </label>
        </div>
        <div class="col input-group mb-2" style="max-width: 230px">
            <span class="input-group-text">月份</span>
            <input class="form-control"
                   type="month"
                   value="@(_selectedMonth.ToString("yyyy-MM"))"
                   format="" @onchange="OnMonthChange" />
        </div>
        <div class="col form-check ms-3 mt-2" style="max-width: 100px">
            <input class="form-check-input" type="checkbox" value="@_fromMonth"
                   checked="checked" @onchange="OnFromMonthChange">
            <label class="form-check-label">
                本月往后
            </label>
        </div>
        <div class="col mt-2">
            @if (_filterProjectDtos != null)
            {
                <span>总项目数:@_filterProjectDtos.Count</span>
            }
        </div>
        @*todo:控制权限*@
        <CompAddButton Roles="admin,pm,mgr" Url="project/add" />
    </div>
</div>

<table class="table table-hover table-striped" style="table-layout: fixed; word-break: break-all;">
    <!--sticky固定位置，top: 6rem-->
    <thead style="position: sticky; top: 6rem; z-index: 1; background-color: White">
    <tr>
        <th style="width: 140px">项目编号</th>
        <th style="width: 35%">项目名称</th>
        <th style="text-align: center">主绘图人</th>
        <th style="text-align: center">项目分类</th>
        <th style="text-align: center">风险等级</th>
        <th style="text-align: center">合同</th>
        <th style="text-align: center">领料单</th>
        <th style="text-align: center">特殊要求</th>
    </tr>
    </thead>
    <tbody>
    @if (_filterProjectDtos.Count == 0)
    {
        <p>...</p>
    }
    else
    {
        @foreach (var project in _filterProjectDtos)
        {
            <ProjectRow ProjectDto="project"/>
        }
    }
    </tbody>
</table>

@code {
    private string _search = string.Empty;
    private bool _annual;
    private bool _fromMonth = true;
    private DateTime _selectedMonth = DateTime.Today;
    private int _year;
    private int _month;

    private List<ProjectDto> _projectDtos = new();
    private List<ProjectDto> _filterProjectDtos = new();
    protected override async Task OnParametersSetAsync()
    {
        _year= _selectedMonth.Year;
        _month= _selectedMonth.Month;
        //过滤条件变化
        await GetData();
        Search(new ChangeEventArgs { Value = _search });
        StateHasChanged();
    }
    private async void OnAnnualChange(ChangeEventArgs e)
    {
        _annual =Convert.ToBoolean(e.Value);
        await GetData();
        Search(new ChangeEventArgs { Value = _search });
        StateHasChanged();
    }
    private async void OnFromMonthChange(ChangeEventArgs e)
    {
        _fromMonth =Convert.ToBoolean(e.Value);
        await GetData();
        Search(new ChangeEventArgs { Value = _search });
        StateHasChanged();
    }
    private async void OnMonthChange(ChangeEventArgs e)
    {
        _selectedMonth = Convert.ToDateTime(e.Value);
        _year= _selectedMonth.Year;
        _month= _selectedMonth.Month;
        await GetData();
        Search(new ChangeEventArgs { Value = _search });
        StateHasChanged();
    }

    private async Task GetData()
    {
        _projectDtos.Clear();
        _projectDtos.AddRange(await ProjectService.GetBlazorProjectsAsync(_year, _month, _annual, _fromMonth));
        //异步方法需要主动告知前台状态已经发生改变
        StateHasChanged();
    }


    private void Search(ChangeEventArgs e)
    {
        _search=e.Value!.ToString()!;
        _filterProjectDtos.Clear();

        _filterProjectDtos.AddRange(_projectDtos.Where(
            x => string.IsNullOrEmpty(_search) ||
                 x.OdpNumber.ToLower().Contains(_search.ToLower()) ||
                 x.Name.ToLower().Contains(_search.ToLower()) ||
                 (!string.IsNullOrEmpty(x.SpecialNotes) && x.SpecialNotes.ToLower().Contains(_search.ToLower()))
            ));
    }
}
